home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / k_r.arc / K&R_CH3.C < prev    next >
Text File  |  1985-11-05  |  4KB  |  399 lines

  1. pr 3.*
  2.  
  3.  
  4. Jul 27 17:12 1984  3.10while.c Page 1
  5.  
  6.  
  7. main()  /* example of nested while statements */
  8. {
  9.         int x = 10, y;
  10.  
  11.         while (x >= 0)
  12.         {       y = x;
  13.                 while (y >= 0)
  14.                 {       printf("%2d%c", y, (y == 0)? '\n': '\040');
  15.                         --y;
  16.                 }
  17.                 --x;
  18.         }
  19. }
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. Jul 27 17:12 1984  3.14for.c Page 1
  71.  
  72.  
  73. main()  /* example of nested for statements */
  74. {       int i, j;
  75.  
  76.         for (i = 0; i < 5; ++i)
  77.         {
  78.                 for (j = 0; j < 5; ++j)
  79.                         printf("%d,%d  ", i, j);
  80.                 putchar('\n');
  81.         }
  82. }
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. Jul 27 17:12 1984  3.20wc.c Page 1
  137.  
  138.  
  139. #define EOF -1
  140. main()  /* emulation of wc(1) without options */
  141. {       int     c, inword;
  142.         long    cc, lc, wc;
  143.         inword = cc = lc = wc = 0;
  144.         while ((c = getchar()) != EOF)
  145.         {       ++cc;
  146.                 switch (c)
  147.                 { case '\n':    ++lc;
  148.                   case '\t':
  149.                   case '\040':  inword = 0; break;
  150.                   default:      if (!inword)
  151.                                         ++wc;
  152.                                 inword = 1;
  153.                 }
  154.         }
  155.         printf("%7ld %6ld %6ld\n", lc, wc, cc);
  156. }
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. Jul 27 17:12 1984  3.24strln.c Page 1
  203.  
  204.  
  205. #define EOF -1
  206. #define MAX 256
  207. main()  /* test strln() */
  208. { char stor[MAX];
  209.   getline(stor, MAX);
  210.   printf("string length including null at end is %d\n", strln(stor));
  211. }
  212. strln(str)      /* return length of char string stored in array str;
  213.                 /* include null at end in count */
  214. char str[];
  215. {       int i = 0, length = 0;
  216.         do
  217.         {       ++length;
  218.         } while (str[i++] != '\0');
  219.         return (length);
  220. }
  221. getline(line, lim)      /* function to read a line from std input;
  222.                         /* insert NULL at end and return char count */
  223. char    line[];         /* array where line will be stored */
  224. int     lim;            /* max line size */
  225. {
  226.   int   c, i;
  227.  
  228.   for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
  229.         line[i] = c;
  230.   if (c == '\n')                /* retain  */
  231.         line[i++] = c;          /* newline */
  232.   line[i] = '\0'; return (i);
  233. }
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. Jul 27 17:12 1984  3.28bc.c Page 1
  269.  
  270.  
  271. main()  /* example of break and continue */
  272. {
  273.         int     x, y;
  274.         for (x = 10; x; --x)
  275.         {       if      (x == 3)
  276.                         break;
  277.                 else    y = x - 1;
  278.                 while   (y)
  279.                 {       if (y == 3)
  280.                         {       --y; continue;
  281.                         }
  282.                         printf("%3d,%d ", x, y--);
  283.                 }
  284.                 putchar('\n');
  285.         }
  286. }
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334. Jul 27 17:12 1984  3.32bcg.c Page 1
  335.  
  336.  
  337. main()  /* example of break, continue, and goto */
  338. {
  339.         int     x, y;
  340.         for (x = 10; x; --x)
  341.         {       if      (x == 3)
  342.                         break;
  343.                 else    y = x - 1;
  344.                 while   (y)
  345.                 {       if (y == 3)
  346.                         {       --y; continue;
  347.                         }
  348.                         else if (x == 5 && y == 1)
  349.                                 goto done;
  350.                         printf("%3d,%d ", x, y--);
  351.                 }
  352.                 putchar('\n');
  353.         }
  354.         done: printf(" DONE!\n");
  355. }
  356.  
  357.  
  358.  
  359.  
  360.  
  361.  
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.